package com.lst.servlet;


import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.DynamicReqPara;
import com.lst.service.DynamicService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: DynamicAddServlet
 * @Description: 动态发布接口
 * @date 2016年9月27日 下午4:30:45
 * 
 */
public class DeleteDynamicServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private DynamicService dynamicService = ctx.getBean(DynamicService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		BaseResponse resPara = new BaseResponse();

		try {
			DynamicReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer dynamicId = Integer.valueOf(reqPara.getDynamicId());					
			dynamicService.updateByPrimaryKey2(dynamicId);
			
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
		//	logger.info("DeleteDynamicServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public DynamicReqPara getReqPara(HttpServletRequest request) {

		DynamicReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new DynamicReqPara());

			reqPara = (DynamicReqPara) baseRequest.clone();

			String dynamicId = request.getParameter("dynamicId");

		//	logger.info("reqPara dynamicId:" + dynamicId);

            reqPara.setDynamicId(dynamicId);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		DynamicReqPara cdReqPara = (DynamicReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getDynamicId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("id为空");
		} else {
			flag = true;
		}

		return  flag;
	}

}
